<?php

namespace app\index\controller;

use app\common\controller\Frontend;

use app\admin\model\article\Article;

use app\admin\model\article\Banner;

use app\admin\model\article\Fenlei;

use think\Cache;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';
    protected $categorylist = [];

    public function index()
    {
        $Article = new Article;



        //分类滚动
        if(!$gundong = Cache::get('gundong'))
        {
            $gundong = Fenlei::all(function($query){
                $query->where(['pid'=>['neq',0]]);
            });
            Cache::set('gundong',$gundong,0);
        }
        $this->assign('gundong',$gundong);

        $Banner = new Banner;
        //幻灯片
        if(!$banner = Cache::get('banner'))
        {
            $banner = collection($Banner->select())->toArray();

            Cache::set('banner',$banner,0);
        }
        $this->assign('banner',$banner);

        $week = Db::name('article')->whereTime('createtime', 'week')->count('id');
        $this->assign('week',$week);

        $list = $Article
            ->with(['fenlei'])
            ->where(['is_shopdata'=>1,'status'=>1])->where("back_image != '' ")
//            ->order($sort, $order)
            ->paginate(10);

        foreach ($list as $row) {
            $row->visible(['id','title','path_imgimage','price','pageviews']);
            $row->visible(['fenlei']);
            $row->getRelation('fenlei')->visible(['title']);
        }

        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $this->assign('list', $list);

        $this->assign('page', $page);


        return $this->view->fetch();
    }

    public function articleList()
    {

        $Article = new Article;
        $list = $Article
            ->with(['fenlei'])
            ->where(['is_shopdata'=>1])
//            ->order($sort, $order)
            ->paginate(10);

        foreach ($list as $row) {
            $row->visible(['id','title','path_imgimage','price']);
            $row->visible(['fenlei']);
            $row->getRelation('fenlei')->visible(['title']);
        }

        //$result = array("total" => $list->total(), "rows" => $list->items());
        return json($list);
    }



}
